package com.efast.dmom.mes.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mes.bean.Customer;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.entity.CustomerEntity;
import com.efast.dmom.mes.entity.base.IModificationFlags;
import com.efast.dmom.mes.service.CustomerService;

/**
 * Implement the customer's new, delete, modify, query methods
 * 
 * @author LYX
 * @date 2020/01/14
 * @Date 2020/2/18 Wj modify
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	@RequestMapping(value = "/operateByCustomer")
	public @ResponseBody ResultBean operateByCustomer(@RequestBody Customer customer) {
		// Determine the new
		
		CustomerEntity entity = customer.getInnerData();
		
		if ( customer.getModificationFlag() == IModificationFlags.NEW) {
			return customerService.save(customer);
			// Judge update
		} else if (customer.getModificationFlag() == IModificationFlags.MODIFIED) {
			return customerService.update(customer);
			// Judge to delete
		} else if (customer.getModificationFlag() == IModificationFlags.DELETED) {
			return customerService.delete(customer);
		}
		return null;
	}

	// get customer by id
	@RequestMapping(value = "/getCustomerById/{id}")
	public @ResponseBody Customer getCustomerById(@PathVariable long id) {
		return customerService.getById(id);
	}

	// get customer by name
	@RequestMapping(value = "/getCustomerByName/{name}")
	public @ResponseBody Customer getCustomerByName(@PathVariable String name) {
		return customerService.getByName(name);
	}

}
